# Copyright (c) 2022, hzmes and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from frappe.utils import (
	add_to_date, today
)

class SRMTrialProduction(Document):

	def before_insert(self):
		if not self.billno:
			current_billno = "GM"
			current_billno += "".join(today().split("-"))
			series = frappe.db.get_value('SRM Naming Series',
								{'doctype_name': self.doctype, 'date': today()},
								['name', 'current_series'], as_dict=True)
			if series:
				current_billno += str(series.current_series).zfill(3)
			else:
				frappe.get_doc({'doctype': 'SRM Naming Series', 'doctype_name': self.doctype, 'date': today()}).insert(ignore_permissions=True)
				current_billno += "001"
			self.billno = current_billno
			frappe.db.set_value('SRM Naming Series', series.name, 'current_series', series.current_series + 1)


	# 定义一个日期计算方法以内部调用
	@frappe.whitelist()
	def calc_date(self, date_from, days, as_string=True, as_datetime=True):
		return add_to_date(date_from, days=int(days))
